#include <string.h>
#include "rtsp_unit.h"
#include "PBStrDispose.h"
#include "rtsp_response.h"
#include "rtsp_resolver.h"


int RTSPInfo_init(RTSPInfo *info) {
    PBufStr_init(&info->url, info->url_buf, RTSPINFO_URL_SIZE);                 //初始化保存资源路径
    PBufStr_init(&info->version, info->version_buf, RTSPINFO_VERSION_SIZE);     //初始化保存版本信息
    PBufStr_init(&info->user_agent, info->user_agent_buf, RTSPINFO_URL_SIZE);   //初始化保存用户代理
}

int RTSPUnit_init(RTSPUnit *unit, RTSPService *service, int c_socket, struct sockaddr_in *c_in) {
    unit->service = service;
    memcpy(&unit->c_in, c_in, sizeof(struct sockaddr_in));                                         //保存客户连接套接字
    BSocketFile_init(&unit->socketfile, unit->socket_recv_buf, RTSPUNIT_RECVBUF_SIZE, c_socket);   //配置套接字读写
    PBufStr_init(&unit->str, (char *) unit->strbuf, RTSPUNIT_BUF_SIZE);                   //读写数据缓冲区
    PBufStr_init(&unit->tmp_str, (char *) unit->tmp_str_buf, RTSPUNIT_TMP_BUF_SIZE);               //SDP描述缓冲区
    RTSPInfo_init(&unit->info);                                                                    //信息缓冲区
    return RTSPUNIT_RET_SUCCEED;
}


//解析协议
int RTSPUnit_resolver(RTSPUnit *unit) {
    int ret = RTSPUNIT_RET_WAIT;
    switch (unit->response_state) {
        case RTSPUNIT_RESPONSE_CMD:
            //解析命令行
            ret = rtsp_resolver_cmd(unit);                              //解析第一行
            if (ret == RTSPUNIT_RET_SUCCEED && (unit->response_state != RTSPUNIT_RESPONSE_DESTROY)) {
                unit->response_state = RTSPUNIT_RESPONSE_PARAMETER;     //转为解析参数
            }
            break;
        case RTSPUNIT_RESPONSE_PARAMETER:
            //解析参数
            ret = rtsp_resolver_arg(unit);                              //解析参数
            if (ret == RTSPUNIT_RET_SUCCEED && (unit->response_state != RTSPUNIT_RESPONSE_DESTROY)) {
                unit->response_state = RTSPUNIT_RESPONSE_RESPONSE;      //响应解析
                unit->unit_state++;
            } else if (ret == RTSPUNIT_RET_ERR) {
                unit->response_state = RTSPUNIT_RESPONSE_CMD;           //解析命令
            }
            break;
        case RTSPUNIT_RESPONSE_RESPONSE:
            switch (unit->unit_state) {
                case RTSPUNIT_STATE_OPTIONS_RESPONSE:                   //响应选项,进行处理
                    ret = rtsp_response_options(unit);
                    break;
                case RTSPUNIT_STATE_DESCRIBE_RESPONSE:                  //响应描述,进行处理
                    ret = rtsp_response_describe(unit);
                    break;
                case RTSPUNIT_STATE_SETUP_RESPONSE:                     //响应设置,进行处理
                    ret = rtsp_response_setup(unit);
                    break;
                case RTSPUNIT_STATE_PLAY_RESPONSE:                      //响应播放,进行处理
                    ret = rtsp_response_play(unit);
                    break;
                case RTSPUNIT_STATE_PAUSE_RESPONSE:                     //响应暂停,进行处理
                    ret = rtsp_response_pause(unit);
                    break;
                case RTSPUNIT_STATE_TEARDOWN_RESPONSE:                  //响应取消,进行处理
                    ret = rtsp_response_teardown(unit);
                    break;
                default:                                                //没有
                    ret = RTSPUNIT_RET_WAIT;
                    unit->response_state = RTSPUNIT_RESPONSE_CMD;
                    break;
            }
            if (ret == RTSPUNIT_RET_SUCCEED && (unit->response_state != RTSPUNIT_RESPONSE_DESTROY)) {
                unit->response_state = RTSPUNIT_RESPONSE_SEND;          //转为发送响应模式
            }
            break;
        case RTSPUNIT_RESPONSE_SEND:    //发送RTSP响应
            ret = BSocketFile_wirte(&unit->socketfile, (unsigned char *) unit->str.buf, unit->str.str_len);     //发送数据
            if (ret == RTSPUNIT_RET_SUCCEED) {
                if (unit->response_state != RTSPUNIT_STATE_TEARDOWN_RESPONSE) {
                    unit->response_state = RTSPUNIT_RESPONSE_CMD;      //发送成功转为解析命令
                } else {
                    unit->response_state = RTSPUNIT_RESPONSE_DESTROY;  //转为销毁状态
                }
                PBufStr_reset(&unit->str);
            }
            break;
        case RTSPUNIT_RESPONSE_DESTROY:     //销毁状态
            ret = rtsp_response_destory(unit);
            break;
    }
    //任务执行有错误，取消任务
    if (ret == RTSPUNIT_RET_ERR) {
        ret = rtsp_response_destory(unit);
    }
    return ret;
}


//解析任务
int RTSPUnit_resolver_task(RTSPUnit *unit) {
    int ret;
    //解析接收数据
    ret = RTSPUnit_resolver(unit);    //解析RTSP
    if (ret == RTSPUNIT_RET_CANCEL) {
        ret = TTHREADTASK_RET_CANCEL;
    } else {
        ret = TTHREADTASK_RET_RUN;
    }
    return ret;
}

